const router = require('koa-router')();
const cc = require('../conCtl');
const js = require('../jsCtl');
const db = require('../dbCtl');
const pc = require('../projectCtl');


/** 获取珠珠儿列表列表
###
    GET {{hostpath}}?page=1&size=30
### */
router.get('/', async c => {
    let
        page = c.query.page || 1,
        size = c.query.size || 30;

    let model = db.model('t_robot').where('state>0');

    let data = await model.orderBy('dt desc').select(page, size);
    let count = await model.count();

    cc.send_list(c, data, count);
});

router.put('/', async c => {
    js.check_params(
        !c.request.body.nick && '!名称' ||
        !c.request.body.headurl && '!头像');

    let data = c.request.body;
    if (!c.request.body.id) delete data.id;

    await db.model('t_robot').insertOrUpdate(data);
    if (!c.request.body.id) pc.storeSettings.robot_count += 1;

    cc.send(c);
});

router.del('/', async c => {
    js.check_params(
        !c.request.body.id && '!ID');

    let data = c.request.body;

    db.model('t_robot').where('id=?', data.id).relyUpdate('删除失败',{
        state: 0
    });
    pc.storeSettings.robot_count -= 1;

    cc.send(c);
});


/********************************winner list*************************************/
router.get('/winnerList', async c => {
    let
        page = c.query.page || 1,
        size = c.query.size || 30;

    let model = db.model('t_robot_winner a')
        .join('t_robot r on a.robot_id = r.id')
        .join('t_auction b on a.type=1 and a.about_id=b.id')
        .join('t_gameLottery c on a.type=2 and a.about_id=c.id')
        .join('t_goods g on a.goods_id=g.id')
        .field('a.type, a.dt, r.nick, r.headurl, b.name as `auctionName`, c.name as `gameLotteryName`, g.name as `goodsName`');

    let data = await model.orderBy('a.dt desc').select(page, size);
    let count = await db.model('t_robot_winner').count();

    cc.send_list(c, data, count);
});




/********************************config*************************************/
router.get('/config', async c => {
    let data = await db.model('t_robot_setting').select();
    cc.send(c, data);
});

//todo... 待修改1：更新projectCtl中的缓存；2：添加dial_condition_number_max字段的处理方法
router.put('/config', async c => {
    js.check_params(
        !c.request.body.robot_auction_freeze && '!拍品冻结时间' ||
        !c.request.body.dial_min_interval && '!九宫格排队最小间隔' ||
        !c.request.body.dial_max_interval && '!九宫格排队最大间隔' ||
        !c.request.body.dial_condition_number && '!九宫格排队条件小于' ||
        !c.request.body.security_fund && '!保证金' ||
        !c.request.body.dial_condition_number_max && '!九宫格排队条件大于');

    await db.start(
        'UPDATE t_robot_setting SET val= CASE item WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? END',
        [
            'robot_auction_freeze', c.request.body.robot_auction_freeze,
            'dial_min_interval', c.request.body.dial_min_interval,
            'dial_max_interval', c.request.body.dial_max_interval,
            'dial_condition_number', c.request.body.dial_condition_number,
            'security_fund', c.request.body.security_fund,
            'dial_condition_number_max', c.request.body.dial_condition_number_max
        ]);

    cc.send(c);
});


module.exports = router;